import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userInfo: null, //用户信息
    pwd: '', //用户密码
    address: null, //收货地址暂存
    atOnceId: 0 , //立即购买时保存的id
    endTime: 0,//账号退出时间
    categoryIndex: 0, //分类索引
  },
  mutations: {
    setUserInfo(state,payload) { //设置用户信息
      state.userInfo  = payload
    },
    saveAddress(state,payload) { //暂存收货地址
      state.address  = payload
    },
    savePwd(state,payload) { //保存密码
      state.pwd  = payload
    },
    setAtOnceId(state,payload) { //设置立即购买时保存的id
      state.atOnceId = payload
    },
    setEndTime(state,payload) { //设置退出时间
      state.endTime = payload
    },
    clearInfo(state,payload) { //清除信息
      state.userInfo = null
      state.pwd = ''
      state.address = null
      state.atOnceId = 0
      state.endTime = 0
    },
    saveCategory(state,payload) { //记录跳转前的分类索引
      state.categoryIndex = payload
    }
  },
  plugins: [createPersistedState( //vuex本地持久化插件
    { storage: window.sessionStorage }
  )]

  
})
